package com.xunshangwang.pinhaohuo.ui.activities.address;

import android.os.Bundle;

import com.alibaba.fastjson.annotation.JSONCreator;
import com.loopj.android.http.RequestParams;
import com.xunshangwang.pinhaohuo.entity.Address;
import com.xunshangwang.pinhaohuo.http.HttpTool;
import com.xunshangwang.pinhaohuo.http.JsonHttpHandler;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;

import cz.msebera.android.httpclient.Header;
import tool.library.ToastTool;

/**
 * Created by xzp on 16/2/16.
 */
public class AddAddressActivity extends BaseAddressActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(savedInstanceState==null)
            mSelectAddressBtn.performClick();
    }

    @Override
    void initAddress() {
        mAddress=new Address();
    }

    @Override
    void requestSave() {
//        RequestParams params=new RequestParams();
//        params.put("province",mAddress.getProvince_id());
//        params.put("city",mAddress.getCity_id());
//        params.put("district",mAddress.getDistrict_id());
//        params.put("mobile",mPhoneEdit.getText().toString());
//        params.put("receive_name",mReceiveNameEdit.getText().toString());
//        params.put("address",mLongAddressEdit.getText().toString());
       // params.put("token",getMainApplication().getToken());
//        params.put("status","COMMON");
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("receive_name",mReceiveNameEdit.getText().toString().trim());
            jsonObject.put("province",mAddress.getProvince_id());
            jsonObject.put("city",mAddress.getCity_id());
            jsonObject.put("district",mAddress.getDistrict_id());
            jsonObject.put("mobile",mPhoneEdit.getText().toString().trim());
            jsonObject.put("address",mLongAddressEdit.getText().toString().trim());
            jsonObject.put("status","COMMON");
        } catch (JSONException e) {
            return;
        }

        HttpTool.postJson(this, "?g=Api&m=Weuser&a=address&token=" + getMainApplication().getToken(), jsonObject , new JsonHttpHandler<Void>() {
            @Override
            public Void parseData(JSONObject object) throws Throwable {
                return null;
            }

            @Override
            public void onStart() {
                super.onStart();
                mLoad.startLoad();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                mLoad.finishLoad();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, Void response) {
                ToastTool.showToast("添加成功");
                setResult(RESULT_OK);
                finish();
            }
        });
    }
}
